/*
 * Copyright Jaanus Heeringson jaanus@heeringson.com
 */
dojo.provide('dojoc.NavMenu');
dojo.provide('dojoc.menuController');
dojo.provide('dojoc.menuNode');

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojo.fx");
dojo.require("dojo.fx.easing");

dojo.declare('dojoc.menuItem', [dijit._Widget], {
    active: false,
    menuNode: false,
    parentController: false,
    parentMenu: false,
    buildRendering: function(){
        var _menu = '';
        dojo.attr(this.srcNodeRef, 'id', this.id);
        dojo.connect(this.srcNodeRef, 'onmouseenter', this, '_onMouseEnter');
        dojo.connect(this.srcNodeRef, 'onmouseleave', this, '_onMouseLeave');
        dojo.connect(this.srcNodeRef, 'onmouseover', this, '_onMouseOver');
        dojo.connect(this.srcNodeRef, 'onmouseout', this, '_onMouseOut');
		var _menu = dojo.query(">ul", this.srcNodeRef)[0];
        if (_menu) {
            this.menuNode = new dojoc.menuNode({
                controller: this,
                baseClass: this.baseClass
            }, _menu);
            console.log('test');
            dojo.connect(this.srcNodeRef, 'onclick', this, '_onClick');
        }
    },
    _onClick: function(e){
        e.stopPropagation();
        this._onSelect();
    },
    _onSelect: function(){
        dojo.addClass(this.srcNodeRef, this.baseClass + 'Selected');
        if (this.menuNode == false) {
            console.log('Lef node - show parent', this.srcNodeRef);
            return this.parentController._onSelect();
        }
        console.log('Controller node', this.srcNodeRef);
        var _anim = this.showMenu();
        console.log(_anim);
        if (_anim.length) 
            dojo.fx.chain(_anim).play();
    },
    showMenu: function(){
        var _anim = [];
        if (this.active) {
            if (this.menuNode != false) 
                var _anim = _anim.concat(this.hideSubMenus());
        }
        else {
            if (this.parentController != false) {
                var _anim = _anim.concat(this.parentController.showMenu());
            }
            if (this.menuNode != false) 
                _anim.push(this.menuNode.showAnim());
        }
        return _anim;
        
    },
    hideMenu: function(){
        if (this.menuNode != false) {
            var _anim = this.hideSubMenus();
            _anim.push(this.menuNode.hideAnim());
            dojo.removeClass(this.srcNodeRef, this.baseClass + 'Selected');
        }
        else {
            var _anim = [];
        }
        return _anim;
    },
    hideSubMenus: function(){
        var _cont = this.menuNode.getControllers();
        console.log('Hide submenus for', this.srcNodeRef, _cont.length);
        for (var i = 0; i < _cont.length; i++) {
            if (_cont[i].getActive()) 
                return _cont[i].hideMenu();
        }
        return [];
    },
    _onMouseEnter: function(e){
        e.stopPropagation();
        dojo.addClass(this.srcNodeRef, this.baseClass + 'OverItem');
        
    },
    _onMouseLeave: function(e){
        dojo.removeClass(this.srcNodeRef, this.baseClass + 'OverItem');
    },
    _onMouseOver: function(e){
        e.stopPropagation();
        dojo.addClass(this.srcNodeRef, this.baseClass + 'OverItem');
        
    },
    _onMouseOut: function(e){
        dojo.removeClass(this.srcNodeRef, this.baseClass + 'OverItem');
    },
    setActive: function(state){
        this.active = state;
    },
    getActive: function(){
        return this.active;
    }
});

dojo.declare('dojoc.menuNode', [dijit._Widget], {
    active: false,
    controller: false,
    buildRendering: function(){
        dojo.attr(this.srcNodeRef, 'id', this.id);
        var self = this;
        this.controllers = dojo.query(">li", this.srcNodeRef).map(function(node){
            dojo.style(node, 'display', 'none');
            return new dojoc.menuItem({
                parentController: self.controller,
                baseClass: self.baseClass
            }, node)
        });
    },
    getControllers: function(){
        return this.controllers;
    },
    showAnim: function(){
        var _node = this.srcNodeRef;
        var _animIn = [];
        this.controllers.forEach(function(e, i){
            _animIn.push(dojo.fx.wipeIn({
                node: e.srcNodeRef,
                duration: 50
            }));
        });
        var _animIn = dojo.fx.chain(_animIn);
        dojo.connect(_animIn, 'beforeBegin', this, function(){
            this.controller.setActive(true);
        })
        return _animIn;
    },
    hideAnim: function(){
        var _animOut = [];
        var _baseClass = this.baseClass;
        this.controllers.forEach(function(e, i){
            _animOut.unshift(dojo.fx.wipeOut({
                node: e.srcNodeRef,
                duration: 50,
                beforeBegin: function(n){
                    dojo.removeClass(n, _baseClass + 'Selected');
                }
            }));
        })
        var _animOut = dojo.fx.chain(_animOut);
        dojo.connect(_animOut, 'onEnd', this, function(){
            this.controller.setActive(false);
        })
        return _animOut;
    }
});

dojo.declare("dojoc.NavMenu", [dojoc.menuItem], {
    baseClass: 'navMenu',
    menuNode: false,
    parentController: false,
    active: false,
    buildRendering: function(){
        dojo.attr(this.srcNodeRef, 'id', this.id);
        var self = this;
        this.menuNode = dojo.query(">ul", this.srcNodeRef).map(function(node){
            return new dojoc.menuNode({
                controller: self,
                active: false,
                baseClass: self.baseClass
            }, node)
        })[0];
        dojo.style(this.menuNode.srcNodeRef, 'visibility', 'visible');
        var _active = dojo.query('[href=\"' + dojo.doc.location.pathname + '\"]');
        if (_active[0] && _active[0].parentNode.id) {
            dijit.byId(_active[0].parentNode.id)._onSelect();
        }
        else {
            dojo.fx.chain(this.showMenu()).play();
        }
    }
});
